package cn.chenxi.hadoopproject.temperature.service.impl;

import cn.chenxi.hadoopproject.base.enums.MonthEnum;
import cn.chenxi.hadoopproject.temperature.mapper.TemperatureMapper;
import cn.chenxi.hadoopproject.temperature.pojo.dto.TemperatureQuery;
import cn.chenxi.hadoopproject.temperature.pojo.entity.Temperature;
import cn.chenxi.hadoopproject.temperature.pojo.vo.TemperatureVO;
import cn.chenxi.hadoopproject.temperature.service.TemperatureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class TemperatureServiceImpl implements TemperatureService {
    @Autowired
    private TemperatureMapper temperatureMapper;

    @Override
    public List<TemperatureVO> select(TemperatureQuery temperatureQuery) {
        log.debug("查询温度信息，查询条件为：{} + {}",temperatureQuery.getCityName(), temperatureQuery.getYear() );
        List<Temperature> temperatures = temperatureMapper.select(temperatureQuery);
        List<TemperatureVO> temperatureVOS = new ArrayList<>();

        // 创建一个 DecimalFormat 实例用于格式化浮点数
        DecimalFormat df = new DecimalFormat("#.00");

        for (Temperature temperature : temperatures) {
            float maxTemp = Float.valueOf(df.format(temperature.getMaxTemp()));
            float minTemp = Float.valueOf(df.format(temperature.getMinTemp()));
            String numMonth = temperature.getDateYear().substring(5, 7);
            int month = Integer.parseInt(numMonth);
            TemperatureVO temperatureVO = new TemperatureVO();
            temperatureVO.setMonth(month);
            temperatureVO.setMaxTemp(maxTemp);
            temperatureVO.setMinTemp(minTemp);
            temperatureVOS.add(temperatureVO);
        }

        return temperatureVOS;
    }
}
